<?php

// @formatter:off
/**
 * @file RandomReaderFactory.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: RandomReaderFactory.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/extensions/RandomReader/includes/RandomReaderFactory.php $
 */
// @formatter:on

class RandomReaderFactory extends WCFactory {
	public static function Checker() {
		//
		// Loading global pointer.
		global $wcProfile;
		//
		// Setting a default value to return.
		$ok = false;
		//
		// Only index (mobile and others are not allowed) and is a user
		// that can read things.
		if(defined("__INDEX__") && $wcProfile->allowedTo(WC_PERM_COMIC_SET_READ)) {
			//
			// This variable will hold a random comic id to show.
			$comicId = false;
			//
			// Loading global pointer.
			global $wcAction;

			$manager = WCViewManager::I();

			//
			// Finding an appropriate comic id;
			switch($wcAction) {
				case WC_ACTION_COLLECTION_SHOW:
					$comicId = RandomReaderFactory::SelectorCOLLECTION();
					if($comicId === false) {
						$comicId = RandomReaderFactory::SelectorANY();
					}
					break;
				case WC_ACTION_TAG_SHOW:
					$comicId = RandomReaderFactory::SelectorTAG();
					if($comicId === false) {
						$comicId = RandomReaderFactory::SelectorANY();
					}
					break;
				case WC_ACTION_DIRECTORY_SHOW:
					$comicId = RandomReaderFactory::SelectorDIRECTORY();
					if($comicId === false) {
						$comicId = RandomReaderFactory::SelectorANY();
					}
					break;
				//case WC_ACTION_COMIC_SHOW:
				//case WC_ACTION_CANVAS:
				case WC_ACTION_SEARCH:
				case WC_ACTION_RANDOM:
				case WC_ACTION_TAG_LIST:
				case WC_ACTION_DIRECTORY_LIST:
				case WC_ACTION_COLLECTION_LIST:
					$comicId = RandomReaderFactory::SelectorANY();
					break;
				default:
					break;
			}
			//
			// Checking if there's something found.
			if($comicId !== false) {
				//
				// Loading global pointer.
				global $wcGroup;
				//
				// Loading selected comic.
				$comic = WCComicsHolder::I()->item($comicId);
				//
				// Checking comic is alive an well.
				if($comic && $comic->ok() && $comic->group == $wcGroup->id) {
					//
					// Setting template variables.
					$manager->WC_E_RANDOMREADER_active = true;
					$manager->WC_E_RANDOMREADER_id = $comic->id;
					$manager->WC_E_RANDOMREADER_name = $comic->name();
					$manager->WC_E_RANDOMREADER_preview = $comic->preview();
					//
					// At this point, there's something to
					// show.
					$ok = true;
				}
			}
		}

		return $ok;
	}
	public static function SelectorANY() {
		//
		// Setting a default value to be returned.
		$out = false;
		//
		// Loading global pointer.
		global $wcGroup;
		global $wcUser;
		//
		// Creating database shortcuts.
		$db = WCConnection::Instance();
		$dbprefix = $db->prefix();
		//
		// Creating a statement to find a random unread comic.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($db->has($stmtId)) {
			$stmt = $db->get($stmtId);
		} else {
			$query = "select  cmc_id\n";
			$query .= "from    {$dbprefix}comics\n";
			$query .= "where   cmc_group = :group\n";
			$query .= " and    cmc_lost  = 'N'\n";
			$query .= " and    cmc_id not in (\n";
			$query .= "                select  crr_comic\n";
			$query .= "                from    {$dbprefix}rr_comics\n";
			$query .= "                where   crr_user  = :user\n";
			$query .= "                 and    crr_group = :group\n";
			$query .= "                 and    crr_read  = 'Y')\n";
			$query .= "order by rand()\n";
			$stmt = $db->prepare($stmtId, $query);
		}
		// @}
		//
		// Building database parameters.
		$params = array(
			":user" => $wcUser->id,
			":group" => $wcGroup->id
		);
		//
		// Asking for unread comics.
		if($stmt->execute($params) && $stmt->rowCount() > 0) {
			$row = $stmt->fetch();
			$out = $row["cmc_id"];
		}
		//
		// Returning results.
		return $out;
	}
	public static function SelectorCOLLECTION() {
		//
		// Setting a default value to be returned.
		$out = false;
		//
		// Loading global pointer.
		global $wcGroup;
		global $wcCollection;
		global $wcUser;
		//
		// Creating database shortcuts.
		$db = WCConnection::Instance();
		$dbprefix = $db->prefix();
		//
		// Creating a statement to find a random unread comic.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($db->has($stmtId)) {
			$stmt = $db->get($stmtId);
		} else {
			$query = "select  cmc_id\n";
			$query .= "from    {$dbprefix}comics\n";
			$query .= "where   cmc_group      = :group\n";
			$query .= " and    cmc_collection = :collection\n";
			$query .= " and    cmc_lost       = 'N'\n";
			$query .= " and    cmc_id not in (\n";
			$query .= "                select  crr_comic\n";
			$query .= "                from    {$dbprefix}rr_comics\n";
			$query .= "                where   crr_user  = :user\n";
			$query .= "                 and    crr_group = :group\n";
			$query .= "                 and    crr_read  = 'Y')\n";
			$query .= "order by rand()\n";
			$stmt = $db->prepare($stmtId, $query);
		}
		// @}
		//
		// Building database parameters.
		$params = array(
			":user" => $wcUser->id,
			":group" => $wcGroup->id,
			":collection" => $wcCollection->id
		);
		//
		// Asking for unread comics.
		if($stmt->execute($params) && $stmt->rowCount() > 0) {
			$row = $stmt->fetch();
			$out = $row["cmc_id"];
		}
		//
		// Returning results.
		return $out;
	}
	public static function SelectorDIRECTORY() {
		//
		// Setting a default value to be returned.
		$out = false;
		//
		// Loading global pointer.
		global $wcGroup;
		global $wcDirectory;
		global $wcUser;
		//
		// Creating database shortcuts.
		$db = WCConnection::Instance();
		$dbprefix = $db->prefix();
		//
		// Creating a statement to find a random unread comic.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($db->has($stmtId)) {
			$stmt = $db->get($stmtId);
		} else {
			$query = "select  cmc_id\n";
			$query .= "from    {$dbprefix}comics\n";
			$query .= "where   cmc_group = :group\n";
			$query .= " and    cmc_directory = :directory\n";
			$query .= " and    cmc_lost  = 'N'\n";
			$query .= " and    cmc_id not in (\n";
			$query .= "                select  crr_comic\n";
			$query .= "                from    {$dbprefix}rr_comics\n";
			$query .= "                where   crr_user  = :user\n";
			$query .= "                 and    crr_group = :group\n";
			$query .= "                 and    crr_read  = 'Y')\n";
			$query .= "order by rand()\n";
			$stmt = $db->prepare($stmtId, $query);
		}
		// @}
		//
		// Building database parameters.
		$params = array(
			":user" => $wcUser->id,
			":group" => $wcGroup->id,
			"directory" => $wcDirectory->id
		);
		//
		// Asking for unread comics.
		if($stmt->execute($params) && $stmt->rowCount() > 0) {
			$row = $stmt->fetch();
			$out = $row["cmc_id"];
		}
		//
		// Returning results.
		return $out;
	}
	public static function SelectorTAG() {
		//
		// Setting a default value to be returned.
		$out = false;
		//
		// Loading global pointer.
		global $wcGroup;
		global $wcTag;
		global $wcUser;
		//
		// Creating database shortcuts.
		$db = WCConnection::Instance();
		$dbprefix = $db->prefix();
		//
		// Creating a statement to find a random unread comic.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($db->has($stmtId)) {
			$stmt = $db->get($stmtId);
		} else {
			$query = "select  cmc_id\n";
			$query .= "from    {$dbprefix}comics\n";
			$query .= "                inner join {$dbprefix}tag_comics\n";
			$query .= "                        on (cmc_group = mem_group && cmc_id = mem_comic)\n";
			$query .= "where   cmc_group = :group\n";
			$query .= " and    mem_tag   = :tag\n";
			$query .= " and    cmc_lost  = 'N'\n";
			$query .= " and    cmc_id not in (\n";
			$query .= "                select  crr_comic\n";
			$query .= "                from    {$dbprefix}rr_comics\n";
			$query .= "                where   crr_user  = :user\n";
			$query .= "                 and    crr_group = :group\n";
			$query .= "                 and    crr_read  = 'Y')\n";
			$query .= "order by rand()\n";
			$stmt = $db->prepare($stmtId, $query);
		}
		// @}
		//
		// Building database parameters.
		$params = array(
			":user" => $wcUser->id,
			":group" => $wcGroup->id,
			":tag" => $wcTag->id
		);
		//
		// Asking for unread comics.
		if($stmt->execute($params) && $stmt->rowCount() > 0) {
			$row = $stmt->fetch();
			$out = $row["cmc_id"];
		}
		//
		// Returning results.
		return $out;
	}
}
